/**
 * 存放公用的service
 * Created by huachuan on 2015/12/24.
 */
angular.module('starter.commonService', [])

  .factory('Storage', function() {
    return {
      //保存数据到本地
      set: function(key, data) {
        return window.localStorage.setItem(key, window.JSON.stringify(data));
      },
      //根据key获取到值
      get: function(key) {
        return window.JSON.parse(window.localStorage.getItem(key));
      },
      //删除本地存储
      remove: function(key) {
        return window.localStorage.removeItem(key);
      }
    };
  })
  //History
  .factory('History',function($ionicHistory){
   return {
     goback:function(){
       $ionicHistory.goBack(-1);
     }
   }
  })
// 启动画面服务
  .service('SplashscreenService', ['$cordovaSplashscreen', function($cordovaSplashscreen) {
    return {
      hide: function () {
        $cordovaSplashscreen.hide();
      },
      show: function () {
        $cordovaSplashscreen.show();
      }
    }
  }])
  // Toast服务
  .service('Toast', ['$cordovaToast','$ionicLoading', function ($cordovaToast,$ionicLoading) {
    return {
      showMessage:function(message){
        $cordovaToast.showShortCenter(message);
      },
      showShortTop: function (message) {
        $cordovaToast.showShortTop(message);
      },
      showShortCenter: function (message) {
        $cordovaToast.showShortCenter(message);
      },
      showShortBottom: function (message) {
       $cordovaToast.showShortBottom(message);
      },
      showLongTop: function (message) {
        $cordovaToast.showLongTop(message);
      },
      showLongCenter: function (message) {
        $cordovaToast.showLongCenter(message);
      },
      showLongBottom: function (message) {
        $cordovaToast.showLongBottom(message);
      }
    }
  }])
  .service('NetworkService', ['$q', '$cordovaNetwork', function ($q, $cordovaNetwork) {
    return {
      // 获取网络类型
      getNetworkType: function () {
        /*
         Connection.UNKNOWN
         Connection.ETHERNET //以太网
         Connection.WIFI	WiFi
         Connection.CELL_2G
         Connection.CELL_3G
         Connection.CELL_4G
         Connection.CELL  //蜂窝网络
         Connection.NONE
         */
        var deferred = $q.defer();
        document.addEventListener("deviceready", function () {
          deferred.resolve($cordovaNetwork.getNetwork())
        }, false);
        return deferred.promise;
      },
      // 是否启用网络
      isOnline: function () {
        var deferred = $q.defer();
        document.addEventListener("deviceready", function () {
          deferred.resolve($cordovaNetwork.isOnline());
        }, false);
        return deferred.promise;
      }
    }
  }])
  .service('DeviceService', ['$q', '$cordovaDevice', function ($q, $cordovaDevice) {
    return {
      //获取用户设备信息
      getDeviceInfo: function () {
        var deferred = $q.defer();
        document.addEventListener("deviceready", function () {
          deferred.resolve("设备名称:" + $cordovaDevice.getModel() + ";运行环境:" + $cordovaDevice.getPlatform() + " " + $cordovaDevice.getVersion());
        }, false);
        return deferred.promise;
      }
    }
  }])
  .service('Loading',['$ionicLoading',function($ionicLoading){

    return{
      showLoading:function(){
        $ionicLoading.show({
          noBackdrop: false,
          template: '<ion-spinner icon="ios"></ion-spinner>'
        });
      },
      hideLoading:function(){
        $ionicLoading.hide();
      }
    }
  }])
;
